package com.herb.phoenix.models;

import com.badlogic.gdx.math.Vector2;

public class Bomb extends EnemyShot
{
	public static final float WIDTH = 0.2f;
	public static final float HEIGHT = 0.2f;
	public static final float SPEED = 3f;
	public static final float TERMINAL_Y = 0.1f;
	public static final float EXPLOSION_DURATION = 0.5f;
	public static final float EXPLOSION_GROWTH_COEFFICIENT = 0.03f; 
	
	public BombState bombState;
	public float alpha;
	public float explosionDuration;
	
	public enum BombState
	{
		FALLING, EXPLODING, EXPLODED
	};
	
	public Bomb()
	{
		position = new Vector2();
		velocity = new Vector2();
		alive = false;
	}
	
	@Override
	public void init(float posX, float posY)
	{
		position.x = posX;
		position.y = posY;
		bounds.x = posX;
		bounds.y = posY;
		bounds.width = WIDTH;
		bounds.height = HEIGHT;
		velocity.set(0, -SPEED);
		
		alive = true;
		bombState = BombState.FALLING;
	}
	
	@Override
	public void reset()
	{
		alive = false;
		bombState = BombState.FALLING;
	}
	
	@Override
	public void update(float delta)
	{
		switch (bombState)
		{
		case EXPLODED:
			explodedUpdate(delta);
			break;
		case EXPLODING:
			explodingUpdate(delta);
			break;
		case FALLING:
			fallUpdate(delta);
			break;
		default:
			break;
		}
	}

	private void explodedUpdate(float delta)
	{
		explosionDuration += delta;
		alpha = (EXPLOSION_DURATION - explosionDuration)/EXPLOSION_DURATION;
		
		if(explosionDuration > EXPLOSION_DURATION)
		{
			alive = false;
		}
	}

	private void explodingUpdate(float delta)
	{
		explosionDuration += delta;
		
		bounds.x -= EXPLOSION_GROWTH_COEFFICIENT;
		bounds.y -= EXPLOSION_GROWTH_COEFFICIENT;
		bounds.width += EXPLOSION_GROWTH_COEFFICIENT*2f;
		bounds.height += EXPLOSION_GROWTH_COEFFICIENT*2f;
		
		if(explosionDuration > EXPLOSION_DURATION)
		{
			setToExploded();
		}
	}

	private void fallUpdate(float delta)
	{
		super.update(delta);
		if(position.y <= TERMINAL_Y)
		{
			bombState = BombState.EXPLODING;
			explosionDuration = 0f;
			alpha = 1f;
		}
	}

	public void setToExploded()
	{
		bombState = BombState.EXPLODED;
		explosionDuration = 0f;
	}
}
